var DB = require('../../../utils/db.js');
var util = require('../../../utils/util.js');

Page({

  data: {
    name:"",
    tabbar: ["扫一扫", "查看记录","搜索"],
    tabIndex: 0,
    inoutList:[],
    value:null,
    search_result:{},
    img:null,
  },

  onLoad: function (options) {
    this.setData({name:options.name});
  },

  onShow: function () {
    //获取时间戳
    this.time = new Date();
    var localtime = Date.parse(this.time)/ 1000;
    // console.log(localtime)
    //时间戳转换
    var date = util.formatTime(this.time);
    // console.log(date)
    // var tim=date.split(" ")[1]
    // console.log(tim)
    this.setData({ time: date,localtime:localtime})
  },

  //上面tabbar
  tabbar: function (e) {
    const _this = this;
    const index = e.currentTarget.dataset.index;
    this.setData({ tabIndex: index });
    if(index == 1){
      wx.showToast({
        title: '努力获取中',
        icon: 'loading',
        mask: true
      })
      //获取数据库INorOUT信息
      DB.getInOut(function(res){
        var inoutList = _this.doList(res);
        _this.setData({ inoutList: inoutList})
      });

    }
  },
                        
  //点击扫码执行方法
  Toscan: function () {
    var _this = this;
    var localtime=this.data.localtime
    wx.scanCode({
      success: (res) => {
        // var t=res.result.split("/")
        var studenttime=res.result.split("/")[1]+'/'+res.result.split("/")[2]+'/'+res.result.split("/")[3]
        // var studentid=res.result.split("/")[3]
        // var time=studentid.split(" ")[1]
        var studentID=res.result.split("/")[0]
        // console.log(studentID)   //切割的学号
        // console.log(time)    //切割的时间
        var timeTamp = Date.parse(studenttime)/ 1000;

        var num=localtime-timeTamp
        var days=(num/86400).toFixed(0)  //转换为多少天


        var remain=num%86400
        var hours=(remain/3600).toFixed(0)


        var min=remain%3600
        var min = (min/60).toFixed(0)


        if(days==0&&hours==0&&min<=5){
        wx.navigateTo({
          url: 'info/info?allID=' + studentID + '&&police=' + _this.data.name+'&&studenttime='+studenttime,
        })
      }else{
        wx.showToast({
          title: '二维码失效',
          icon:'none'
        })
      }
      }
    })
  },

  //处理数组
  doList:function(list){
    var timestamp;
    var identity;
    var inout;
    for(var i=0;i<list.length;i++){
      list[i].timestamp = this.doTime(list[i].timestamp);
      list[i].identity = this.doIdentity(list[i].identity);
      list[i].inorout = this.doInout(list[i].inorout);
    }
    return list.reverse();
  },
  //时间戳转换
  doTime:function(timestamp){
    var date = new Date(timestamp*1000); 
    var y = date.getFullYear(); 
    var m = date.getMonth() + 1; 
    m = m < 10 ? ('0' + m) : m; 
    var d = date.getDate(); d = d < 10 ? ('0' + d) : d; 
    var h = date.getHours(); h = h < 10 ? ('0' + h) : h; 
    var minute = date.getMinutes(); 
    var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; 
    second = second < 10 ? ('0' + second) : second; 
    return m + '/' + d + ' ' + h + ':' + minute;
  },
  //身份转换
  doIdentity:function(identity){
    if(identity == 1){
      return "本校学生";
    } else if (identity == 2 ){
      return "本校教工";
    }else{
      return "外来人员";
    }
  },
  //进出转换
  doInout: function (inout) {
    if (inout == 0) {
      return "正门进校";
    } else if (inout == 1) {
      return "正门出校";
    } else if (inout == 2) {
      return "南门进校";
    } else if (inout == 3) {
      return "南门出校";
    }
  },

  onChange:function(e) {
    this.setData({
      value: e.detail
    });
  },
  onSearch:function(){
    var value = this.data.value;
    if(value == null || value == ""){
      wx.showToast({
        title: '搜索值为空！',
        icon:'none'
      })
    }else{
      wx.showToast({
        title:'搜索中',
        icon:'loading',
        mark:true
      })
      wx.cloud.callFunction({
        name:'get_search_info',
        data:{value:value},
        success:res=>{
          if(res.result.length == 1){
            console.log(res.result[0])
            this.setData({search_result:res.result[0]})
            if (res.result[0].identity == 1){
              var img = "http://class.sise.com.cn:7001/sise/module/studentphoto/" + res.result[0].grade + "/" + res.result[0].studentID + ".jpg"
              this.setData({img:img})
            }
          }else{
            wx.showToast({
              title: '搜索结果为空！',
              icon: 'none'
            })
          }
        }
      })
    }
  }

})